import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/server/http/request_header.dart';
import 'package:ball_lion/utils/app_config.dart';
import 'package:ball_lion/utils/constant.dart';
import 'package:ball_lion/routes/routes.dart';
import 'package:ball_lion/utils/toast_utils.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/utils/widget_utils.dart';
import 'package:ball_lion/widgets/ball_appbar.dart';
import 'package:ball_lion/module/mine/widgets/list_cell_box.dart';
import 'package:ball_lion/widgets/base/wqs_button.dart';
import 'package:url_launcher/url_launcher_string.dart';

import 'package:ball_lion/widgets/base/wqs_text.dart';

class MyAboutUsPage extends StatefulWidget {
  const MyAboutUsPage({super.key});

  @override
  State<MyAboutUsPage> createState() => _MyAboutUsPageState();
}

class _MyAboutUsPageState extends State<MyAboutUsPage> {
  String appName = "";
  String version = "";
  String channel = "";
  int bottomTapNum = 0;
  double firstTap = 0;

  @override
  void initState() {
    PackageInfo.fromPlatform().then((info) {
      appName = info.appName;
      version = "当前版本:${info.version}";
      update();
    });
    channel = AppConfig.config.channel ?? "";
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.scaffoldBg,
      appBar: const BallAppBar(title: "关于我们"),
      body: Stack(
        children: [
          Column(
            children: [
              const SizedBox(height: 8),
              header(),
              const SizedBox(height: 8),
              Padding(padding: const EdgeInsets.symmetric(horizontal: 8),
                child: cellBox([
                  cell("服务协议").tap(() {
                    Get.toNamed(Routes.webview,
                        arguments: {"url": Constant.serviceAgreementUrl});
                  }),
                  const Divider(
                      height: 0.5, color: Colours.N3, indent: 12, endIndent: 12),
                  cell("隐私政策").tap(() {
                    Get.toNamed(Routes.webview,
                        arguments: {"url": Constant.privacyPolicyUrl});
                  }),
                  // const Divider(
                  //     height: 0.5, color: Colours.N3, indent: 12, endIndent: 12),
                  // cell("用户充值协议").tap(() {
                  //   Get.toNamed(Routes.webview,
                  //       arguments: {"url": Constant.payPolicyUrl});
                  // }),
                  // const Divider(
                  //     height: 0.5, color: Colours.N3, indent: 12, endIndent: 12),
                  // cell("自媒体服务用户协议").tap(() {
                  //   Get.toNamed(Routes.webview,
                  //       arguments: {"url": Constant.selfmediaPolicyUrl});
                  // }),
                  const Divider(
                      height: 0.5, color: Colours.N3, indent: 12, endIndent: 12),
                  cell("未成年协议隐私保护协议").tap(() {
                    Get.toNamed(Routes.webview,
                        arguments: {"url": Constant.less18PolicyUrl});
                  }),
                  //   const Divider(
                  //       height: 0.5, color: Colours.N3, indent: 12, endIndent: 12),
                  //   cell("第三方SDK共享个人信息清单").tap(() {
                  //     Get.toNamed(Routes.webview,
                  //         arguments: {"url": Constant.thirdShareUrl});
                  //   }),
                  // ]).marginSymmetric(horizontal: 8),
                  // const QsText(
                  //   "APP备案编号：桂ICP备2023005192号-6A",
                  //   fontSize: 12,
                  //   color: Colours.grey99,
                  // ).paddingOnly(top: 32),
                  // RichText(
                  //     text: TextSpan(
                  //         style:
                  //             const TextStyle(fontSize: 12, color: Colours.grey99),
                  //         children: [
                  //       const TextSpan(text: "查询链接："),
                  //       TextSpan(
                  //           text: "https://beian.miit.gov.cn/",
                  //           style: const TextStyle(color: Color(0xFF5DBBEA)),
                  //           recognizer: TapGestureRecognizer()
                  //             ..onTap = () => launchUrlString(
                  //                 "https://beian.miit.gov.cn/",
                  //                 mode: LaunchMode.externalApplication))
                ]),
              )
            ],
          ),
          Positioned(
              left: 0,
              bottom: 0,
              child: SizedBox(
                width: 60,
                height: 60,
                child: QsButton(
                  onPressed: () {
                    onTap();
                  },
                ),
              ))
        ],
      ),
    );
  }

  Widget header() => Styles.card(
          child: Container(
        height: 200,
        width: double.infinity,
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(8)),
        child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
          SizedBox(
              width: 80,
              height: 80,
              child: Image.asset(Utils.getImgPath("app_logo.png"))),
          const SizedBox(height: 12),
          QsText(appName,
              fontSize: 20, fontWeight: FontWeight.w500, color: Colours.N8),
          const SizedBox(height: 2),
          QsText(version + (channel.isNotEmpty ? " ($channel)" : ''),
              color: Colours.N9, fontSize: 12)
        ]),
      )).marginSymmetric(horizontal: 8);

  Widget cellBox(List<Widget> children) => ListCellBox(children: children);
  // Container(
  //   clipBehavior: Clip.hardEdge,
  //   margin: const EdgeInsets.symmetric(horizontal: 8),
  //   decoration: BoxDecoration(
  //     color: Colours.white,
  //     borderRadius: BorderRadius.circular(8)
  //   ),
  //   child: Column(
  //     children: children,
  //   ),
  // );

  Widget cell(String text, {Widget? right}) => Container(
        padding: const EdgeInsets.symmetric(horizontal: 12),
        height: 46,
        child: Row(
          children: [
            QsText(text, fontSize: 14, color: Colours.N8),
            const Spacer(),
            if (right != null) right,
            if (right == null)
              Image.asset(Utils.getImgPath("icon_arrow_right.png"),
                  width: 6, height: 10)
          ],
        ),
      );

  void onTap() {
    bottomTapNum++;
    final currentTime =
        DateTime.now().millisecondsSinceEpoch.toDouble() / 1000.0;

    if (bottomTapNum == 1) {
      firstTap = currentTime;
    } else if (bottomTapNum == 5) {
      if ((currentTime - firstTap) < 2) {
        Toast.text(RequestHeader.env);
      }
    }
  }
}
